class Solution {
public:
    vector<int> asteroidCollision(vector<int>& asteroids) {
        vector<int> sk;
        for (auto x : asteroids) {
            sk.push_back(x);
            while (sk.size() >= 2) {
                int b = sk.back();
                sk.pop_back();
                int a = sk.back();
                sk.pop_back();
                if (a * b > 0 || (a < 0 && b > 0)) {
                    sk.push_back(a);
                    sk.push_back(b);
                    break;
                }
                if (abs(a) == abs(b)) break;
                else sk.push_back((abs(a) > abs(b) ? a : b));
            }
        }
        return sk;
    }
};
